package mya.mes.mdc.internal.ui;

import com.fasterxml.jackson.databind.node.ObjectNode;
import mya.common.jaxrs.base.JsonCallback;
import mya.common.jaxrs.op.OpGet;
import mya.common.jaxrs.result.ListResult;
import mya.common.jpa.context.AbstractQuery;
import mya.common.jpa.context.ServiceContext;
import mya.common.jpa.pagination.DataPage;
import mya.common.model.base.Jsonable;
import mya.mes.mdc.local.alarm.AlarmLocal;
import mya.mes.mdc.logic.alarm.AlarmLocalImpl;
import mya.mes.mdc.model.alarm.MdcAlarm;
import mya.mes.mdc.model.alarm.MdcAlarmHis;
import mya.mes.mdc.model.alarm.MdcAlarmType;
import org.apache.commons.lang3.StringUtils;

import javax.ws.rs.*;
import java.util.List;

@Path("/{tenantId}/{loginId}/mdcalarm/ui/")
public class MdcAlarmUi {

    @PathParam("tenantId")
    private String tenantId;

    @PathParam("loginId")
    private Long loginId;

    @GET
    @Path("queryMdcAlarm/")
    @Produces({"application/json"})
    public ListResult queryMdcAlarm(
            @QueryParam("q") String qParam,
            @QueryParam("filter") final String filterParam,
            @QueryParam("sort") final String sortParam,
            @QueryParam("start") final Integer start,
            @QueryParam("limit") final Integer limit) {
        OpGet opGet = new OpGet(tenantId, loginId);
        opGet.jsonCallback(new JsonCallback<MdcAlarm>() {
            @Override
            public ObjectNode putValues(ServiceContext serviceContext, ObjectNode objectNode, MdcAlarm mdcAlarm) throws Exception {
                if(mdcAlarm.getAlarmTypeId()!=null){
                    MdcAlarmType alarmType = serviceContext.em.find(MdcAlarmType.class,mdcAlarm.getAlarmTypeId());
                    objectNode.putPOJO("alarmType",alarmType);
                }
                return objectNode;
            }
        });

        String filter = "";
        if(StringUtils.isNotBlank(filterParam)){
            filter = filterParam.replace("endDate","alarmDate");
        }
        return opGet.result(MdcAlarm.class.getSimpleName(), qParam, filter, sortParam, start, limit);
    }

    @GET
    @Path("queryMdcAlarm_/")
    @Produces({"application/json"})
    public ListResult queryMdcAlarm_(
            @QueryParam("q") String qParam,
            @QueryParam("filter") final String filterParam,
            @QueryParam("sort") final String sortParam,
            @QueryParam("start") final Integer start,
            @QueryParam("limit") final Integer limit) {
        OpGet opGet = new OpGet(tenantId, loginId);
        opGet.jsonCallback(new JsonCallback<MdcAlarmHis>() {
            @Override
            public ObjectNode putValues(ServiceContext serviceContext, ObjectNode objectNode, MdcAlarmHis mdcAlarm) throws Exception {
                if(mdcAlarm.getAlarmTypeId()!=null){
                    MdcAlarmType alarmType = serviceContext.em.find(MdcAlarmType.class,mdcAlarm.getAlarmTypeId());
                    objectNode.putPOJO("alarmType",alarmType);
                }
                return objectNode;
            }
        });
        String filter = "";
        if(StringUtils.isNotBlank(filterParam)){
            filter = filterParam.replace("endDate","alarmDate");
        }
        return opGet.result(MdcAlarmHis.class.getSimpleName(), qParam, filter, sortParam, start, limit);
    }

    @GET
    @Path("queryMdcAlarmHis/")
    @Produces({"application/json"})
    public ListResult queryMdcAlarmHis(
            @QueryParam("q") String qParam,
            @QueryParam("filter") final String filterParam,
            @QueryParam("sort") final String sortParam,
            @QueryParam("start") final Integer start,
            @QueryParam("limit") final Integer limit) {
        OpGet opGet = new OpGet(tenantId, loginId);
//        opGet.jsonCallback(new JsonCallback<MdcAlarmHis>() {
//            @Override
//            public ObjectNode putValues(ServiceContext serviceContext, ObjectNode objectNode, MdcAlarmHis mdcAlarm) throws Exception {
//                if (mdcAlarm.getAlarmTypeId() != null) {
//                    MdcAlarmType alarmType = serviceContext.em.find(MdcAlarmType.class, mdcAlarm.getAlarmTypeId());
//                    objectNode.putPOJO("alarmType", alarmType);
//                }
//                return objectNode;
//            }
//        });
        String filter = "";
        if(StringUtils.isNotBlank(filterParam)){
            filter = filterParam.replace("endDate","alarmDate");
        }
        return opGet.result(MdcAlarmHis.class.getSimpleName(), qParam, filter, sortParam, start, limit);
    }


    @GET
    @Path("queryMdcAlarmChart/{type}/{startDate}/{endDate}")
    @Produces({"application/json"})
    public ListResult queryMdcAlarmChart(
            @PathParam("type") final Integer type,
            @PathParam("startDate") final String startDate,
            @PathParam("endDate") final String endDate
    ) {

        AbstractQuery query = new AbstractQuery(tenantId,loginId) {
            @Override
            protected DataPage process(ServiceContext serviceContext) throws Exception {

                AlarmLocal service = new AlarmLocalImpl();

                List list = service.queryAlarmChart(serviceContext,type,startDate,endDate);

                return DataPage.buildFromList(list);
            }
        };

        return ListResult.result(query);
    }


}
